package org.example.com.mapper;

import org.apache.ibatis.annotations.*;
import org.example.com.pojo.vo.ChatMessage;

import java.util.List;

@Mapper
public interface ChatMessageMapper {
    
    // 保存聊天消息
    @Insert("INSERT INTO chat_messages (user_id, role, content, session_id, created_at) " +
            "VALUES (#{userId}, #{role}, #{content}, #{sessionId}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(ChatMessage chatMessage);
    
    // 根据会话ID查询消息历史
    @Select("SELECT id, user_id, role, content, session_id, created_at " +
            "FROM chat_messages WHERE session_id = #{sessionId} " +
            "ORDER BY created_at ASC")
    List<ChatMessage> findBySessionId(String sessionId);
    
    // 根据用户ID查询所有消息
    @Select("SELECT id, user_id, role, content, session_id, created_at " +
            "FROM chat_messages WHERE user_id = #{userId} " +
            "ORDER BY created_at DESC")
    List<ChatMessage> findByUserId(Integer userId);
    
    // 删除指定会话的所有消息
    @Delete("DELETE FROM chat_messages WHERE session_id = #{sessionId}")
    void deleteBySessionId(String sessionId);
    
    // 根据ID删除消息
    @Delete("DELETE FROM chat_messages WHERE id = #{id}")
    void deleteById(Integer id);
}
